﻿// ActionScript file
package org.nickro.controls{
	import caurina.transitions.*;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class VScrollBar extends BaseControls{
		private var upArrow:Sprite;
		private var downArrow:Sprite;
		private var track:Sprite;
		private var thumb:Sprite;
		private var _mask:Sprite;
		private var con:Sprite;
		private var conCopyed:Sprite;
		private var scrollDirection:String;
		private var changed:Boolean=false;
		private var mousePos:Number;
		private var maskInteractive:Sprite;
		/**------------------------
		*public properties;
		------------------------**/

		/**skin�?可以是类对象，也可以是其他显示对象�?
		 * 上下左右箭头、背景轨迹和拖拽�?数据类型为类，可为自定义类或flash库中的类�?*/
		
		private var _scrollBarUpArrow:*;
		private var _scrollBarDownArrow:*;
		private var _scrollBarTrack:*;
		//遮罩图形�?
		private var _maskShape:*;
		//是否显示scrollBar，有三个�?auto,off,on.
//		protected var verticalScrollPolicy:String="auto";
		//滚动速率
		private var _scrollSpeed:Number;
		private var _scrollV:Number;
		private var _content:*;
		private var _mode:String;
		/**------------------------------
		 * constructor
		 * 
		----------------------------  **/
		public function VScrollBar(maskShape:*=null,
										scrollBarDownArrow:*=null,
										scrollBarUpArrow:*=null){
			initInteractive();
											
			if(maskShape != null){
				_maskShape=maskShape;
				if(_maskShape is Class){
					_mask=new _maskShape;
				}
				if(_maskShape is DisplayObject){
					_mask= _maskShape;
				}
				buildMaskShape();
			}
			if(scrollBarDownArrow != null){
				_scrollBarDownArrow=scrollBarDownArrow;
				if(_scrollBarDownArrow is Class){
					downArrow=new _scrollBarDownArrow;
				}
				if(_scrollBarDownArrow is DisplayObject){
					downArrow= _scrollBarDownArrow;
				}
			}
			if(scrollBarUpArrow != null){
				_scrollBarUpArrow=scrollBarUpArrow;
				if(_scrollBarUpArrow is Class){
					upArrow=new _scrollBarUpArrow;
				}
				if(_scrollBarUpArrow is DisplayObject){
					upArrow= _scrollBarUpArrow;
				}
			}
			super();
		}
		
		private function initInteractive():void{
			addEventListener(Event.ADDED_TO_STAGE,onAdded);	
		}
		
		private function buildContent():void{
			con.mask=_mask;
			_mask.x=con.x;
			_mask.y=con.y;

			changed=true;
			updateVScrollBar();
			
		}
		override protected function createChildren():void{
			buildMaskShape();
			buildVScrollBar();
		}
		
		//创建遮罩
		private function buildMaskShape():void{
			addChild(_mask);
		}
		
		//创建滚动�?
		private function buildVScrollBar():void {
			if (upArrow != null && downArrow != null) {
				addVScrollBar();
			}else return;			
		}
		private function addVScrollBar():void {
			addChild(upArrow);
			upArrow.addEventListener(MouseEvent.MOUSE_DOWN,onUpMouseDown);
			
			addChild(downArrow);
			downArrow.addEventListener(MouseEvent.MOUSE_DOWN,onDownMouseDown);
			measureVScrollBar();
		}
		
		/**
		 * 
		 * #VSrollBar的定�?考虑是否将其定义为外部接�?
		 */
		protected function measureVScrollBar():void{
		
		}
		
		//重绘滚动栏。根据con的位置来更新滚动�?
		private function updateVScrollBar():void {
			if(upArrow !=null && downArrow != null && thumb != null && track != null){
				if (con.y >= 0) {
					upArrow.visible=false;
				}else {
					upArrow.visible=true;
				}
				if (con.y <= (_mask.height- con.height+8)) {
					downArrow.visible=false;
				}else {
					downArrow.visible=true;
				}
				if(changed==true){
					updateScrollBarThumb();
				}
				/**
				 * @添加thumb、track的更新函�?
				 */
			}	
		}
		
		//更新/重绘滑块
		protected function updateScrollBarThumb():void{
		}
		
		/**-------------------------------------------------
		 * MOUSE_LOOP
		 ---------------------------------------------------*/
		
		private function activeItem():void {
			if (changed == true) {
				//trace((con.height/_scrollSpeed)*((_mask.height/2)/_scrollV)+"T");
				var _time:Number=(con.height/_scrollSpeed)*((_mask.height/2)/Math.abs(_scrollV));
				if(scrollDirection == "up"){
					Tweener.addTween(con,{y:-con.height,time:_time,transition:"easeOutExpo",
												onUpdate:tweenStop,onComplete:tweenComplete});
				}
				if(scrollDirection == "down"){
					Tweener.addTween(con,{y:0,time:_time,transition:"easeOutExpo",
												onUpdate:tweenStop,onComplete:tweenComplete});
				}
			}
			updateVScrollBar();
		}
		private function tweenStop():void{
			if(con.y>_mask.y ){
				Tweener.removeAllTweens();
				con.y=_mask.y;
			}
			if(con.y<=(_mask.height-con.height+5)){
				Tweener.removeAllTweens();
				con.y=(_mask.height-con.height);
			}
			updateVScrollBar();
		}
		private function tweenComplete():void{
			updateVScrollBar();
		}
		/**-------------------------------------------
		 * SELF_LOOP(自循环运�?
		 * 初始想法�?在本类中直接创建content副本，需要深度拷贝Sprite的知�?好像无解)
		 *     妥协方法:外部用一个容器包含两个content
		 ---------------------------------------------*/
		private function loop():void{	
			Tweener.addTween(con,{y:-con.height+_mask.height,
										time:con.height/_scrollSpeed,
										transition:"linear"});
		}
		
		
		
		/**-----------------------------------------
		Event Handlers
		-------------------------------------------*/
		private function onAdded(e:Event):void{
			
		}
		
		private function onUpMouseDown(e:MouseEvent):void {
			scrollDirection="up";
			changed=true;
			activeItem();
		}
		private function onDownMouseDown(e:MouseEvent):void {
			scrollDirection="down";
			changed=true;
			activeItem();
		}
		
		private function onMaskOver(e:MouseEvent):void{
			//??不执�?
			addEventListener(Event.ENTER_FRAME,onLoop);
		}
		private function onMaskOut(e:MouseEvent):void{
			removeEventListener(Event.ENTER_FRAME,onLoop);
			stop();
		}
		private function onLoop(e:Event):void{
			var dScrollY:Number=_mask.mouseY-(_mask.height/2);
				if(mousePos != dScrollY){
					mousePos=dScrollY;
					if(dScrollY <0){
						tweenStop();
						scrollDirection="up";
						_scrollV=dScrollY;
						activeItem();
					}
					if(dScrollY >0){
						tweenStop();
						scrollDirection="down";
						_scrollV=dScrollY;
						activeItem();
					}
					if(dScrollY == 0){
						tweenStop();
					} 
				}else{
					return;
				}
		}
		/**-------------------------------
		 * public Methods
		 -----------------------------*/
		public function stop():void{
			Tweener.pauseAllTweens();
		}
		public function play():void{
			Tweener.resumeAllTweens();
		}
		
		private function mouseDirection():void{
			con.addEventListener(MouseEvent.MOUSE_OVER,onMaskOver);
			con.addEventListener(MouseEvent.MOUSE_OUT,onMaskOut);
		}
		
	/**	----------------------------------
		public getter/setter
		-------------------------------- */
		//大多数情况下，scrollRect和实例的宽高是一致的�?
		//scrollRect缩小，对container设置滤镜或其他，通过对container中的子项点击-->传递参�?->
		//ScrollContainer.active()滚动con;实现类似滚动效果�?
		//
		public function set scrollBarUpArrow(value:*):void{
			_scrollBarUpArrow=value;
			if(_scrollBarUpArrow is Class){
				upArrow=new _scrollBarUpArrow;
			}
			if(_scrollBarUpArrow is DisplayObject){
				upArrow= _scrollBarUpArrow;
			}
			buildVScrollBar();
		}
		public function get scrollBarUpArrow():Class{
			return _scrollBarUpArrow;
		}
		public function set scrollBarDownArrow(value:Class):void{
			_scrollBarDownArrow=value;
			if(_scrollBarDownArrow is Class){
				upArrow=new _scrollBarDownArrow;
			}
			if(_scrollBarDownArrow is DisplayObject){
				upArrow= _scrollBarDownArrow;
			}
			buildVScrollBar();
		}
		public function get scrollBarDownArrow():Class{
			return _scrollBarDownArrow;
		}
		public function set maskShape(value:*):void{
			_maskShape=value;
			if(_maskShape is Class){
				_mask=new _maskShape;
			}
			if(_maskShape is DisplayObject){
				_mask= _maskShape;
			}
			buildMaskShape();
		}
		//滚动的距�?
		public function set scrollV(value:Number):void{
			_scrollV=value;
			activeItem();
		}
		//滚动的速度    px/s
		public function set scrollSpeed(value:Number):void{
			_scrollSpeed=value;
		}
		public function set content(value:*):void{
			_content=value;
			if(_content is Class){
				con = new _content;
			}
			if(_content is DisplayObject){
				con = _content;
			}
			buildContent();
		}
		public function set mode(value:String):void{
			_mode=value;
			switch(_mode){
				//自动循环运动
				case "SELF_LOOP":
					loop();
					break;
				//鼠标触发运动
				case "MOUSE_LOOP":
					mouseDirection();
					break;
			}	
		}		
	}
}